import React, { Component } from 'react'
import './index.css'

export default class Footer extends Component {

  //  处理全部选中的 
  handleCheckAll = (event) => {
    const { todos } = this.props
    const done = event.target.checked
    const newTodos = todos.map(todo => {
      return { ...todo, done }
    })
    this.props.updateTodos(newTodos)

  }

  handleClearAll = () => {
    const { todos } = this.props
    const total = todos.length
    if (total ===0 )return 
    if(window.confirm('确定全部删除吗？')){
   
      const newTodos= todos.filter(todo=>{
        return !todo.done
      })
      this.props.updateTodos(newTodos)
    }

   
  }
  render() {
    const { todos } = this.props

    const doneCount = todos.reduce((pre, todo) => { return pre + (todo.done ? 1 : 0) }, 0)
    //总数
    const total = todos.length
    return (
      <div className='todo-footer'>

        <input type="checkbox" checked={doneCount === total && total !== 0} onChange={this.handleCheckAll} />
        <span>已完成({doneCount})/全部({total})</span>
        <button onClick={this.handleClearAll} className='btn btn-danger'>清除已完成任务</button>
      </div>
    )
  }
}
